/**
 * @class Spark.CheckBox
 * @extends Spark.Component
 * <p>可选组件.
 * @author njq.niu@hand-china.com
 * @constructor
 * @param {Object} config 配置对象. 
 */
$S.CheckBox = Ext.extend($S.ViewComponent,{
	checkedCss:'s-ckb-c',
    hoverCls:'s-ckb-hover',
//	uncheckedCss:'s-ckb-u',
	readonyCheckedCss:'s-ckb-ro-c',
	readonlyUncheckedCss:'s-ckb-ro-u',
	constructor: function(config){
		config.checked = config.checked || false;
		config.disabled = config.disabled || false;
		$S.CheckBox.superclass.constructor.call(this,config);
	},
	initComponent:function(config){
		this.checkedValue = 'Y';
		this.unCheckedValue = 'N';
		$S.CheckBox.superclass.initComponent.call(this, config);
		this.wrap=Ext.get(this.id);
		this.el=this.wrap.child('div[stype=checkbox]');
        this.initStatus();
	},
	processListener: function(ou){
    	this.wrap[ou]('click',this.onClick,this);
    },
	initEvents:function(){
		$S.CheckBox.superclass.initEvents.call(this);  	
		this.addEvents(
		/**
         * @event click
         * 鼠标点击事件.
         * @param {Spark.CheckBox} checkBox 可选组件.
         * @param {Boolean} checked 选择状态.
         */
		'click');    
	},
	destroy : function(){
    	$S.CheckBox.superclass.destroy.call(this);
    	delete this.el;
    },
	onClick: function(e){
		if(this.wrap.dom != e.target && !this.disabled){
			this.checked = this.checked ? false : true;				
			this.setValue(this.checked);
			this.fireEvent('click', this, this.checked);
		}
	},
    focus : function(){
        this.el.focus();
    },
    blur : function(){
        this.el.blur();     
    },
    onFocus : function(){
        var sf = this;
        sf.el.addClass(sf.focusCss);
        sf.fireEvent('focus',sf);
    },
    onBlur : function(){
        var sf = this;
        sf.el.removeClass(sf.focusCss);
        sf.fireEvent('blur',sf);
    },
	setValue:function(v, silent){
		if(typeof(v)==='boolean'){
			this.checked=v?true:false;			
		}else{
			this.checked = (''+v == ''+this.checkedValue)
//			this.checked = v === this.checkedvalue ? true : false;
		}
		this.initStatus();
		var value = this.checked==true ? this.checkedValue : this.unCheckedValue;		
		$S.CheckBox.superclass.setValue.call(this,value, silent);
	},
	getValue : function(){
    	var v= this.value;
		v=(v === null || v === undefined ? '' : v);
		return v;
    },
//	setDisabled:function(b){
//		if(typeof(b)==='boolean'){
//			this.disabled=b?true:false;	
//			this.initStatus();		
//		}		
//	},
    onMouseOver: function(e){
        $S.CheckBox.superclass.onMouseOver.call(this,e);
        if(!this.disabled)
            this.el.addClass(this.hoverCls);
       
    },
    onMouseOut: function(e){
        $S.CheckBox.superclass.onMouseOut.call(this,e);
        if(!this.disabled)
            this.el.removeClass(this.hoverCls);
    },
    doDisabled :function(disabled){
        if(disabled){
            this.el.addClass(this.disabledCss);
        }else{
            this.el.removeClass(this.disabledCss);
        }
    },
	initStatus:function(){
        $S.CheckBox.superclass.initStatus.call(this);
		this.el.removeClass(this.checkedCss);
        if(this.checked) this.el.addClass(this.checkedCss);	
	}			
});